package net.renfei.indexing.ui;

import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import net.renfei.indexing.entity.ConfigVO;
import net.renfei.indexing.service.ConfigFileService;
import net.renfei.indexing.service.ExecService;
import net.renfei.indexing.service.ExtractSitemapService;
import net.renfei.sdk.utils.BeanUtils;
import net.renfei.sdk.utils.DateUtils;

import javax.swing.*;
import javax.swing.text.BadLocationException;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import static javax.swing.JOptionPane.WARNING_MESSAGE;

/**
 * 主窗体
 *
 * @author renfei
 */
public class MainWindow {
    public JPanel mainPanel;
    public JTextArea urls;
    public JTextArea logText;
    public JCheckBox chkBaiduPuTong;
    public JCheckBox chkGoogle;
    public JCheckBox chkBaiDuKuiSu;
    public JButton execButton;
    public JButton googleJson;
    public JCheckBox chkBing;
    public JTextField siteUrl;
    public JTextField baiduToken;
    public JTextField bingToken;
    public JTextArea explain;
    public JSplitPane rightSplitPane;
    public JScrollPane urlsScroPane;
    public JScrollPane logsScroPane;
    public JCheckBox saveConfig;
    public JTextField soToken;
    public JCheckBox chkSo;
    private JButton extractSitemapButtonButton;

    public void init() {
        urlsScroPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        logsScroPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        MainWindow mainWindow = this;
        execButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setLog("开始执行");
                Thread execService = new Thread(new ExecService(mainWindow), "ExecService");
                execService.start();
            }
        });
        googleJson.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fc = new JFileChooser("/");
                int val = fc.showOpenDialog(null);
                if (val == JFileChooser.APPROVE_OPTION) {
                    googleJson.setText(fc.getSelectedFile().getPath());
                } else {
                    googleJson.setText("点击选择JSON文件");
                }
            }
        });
        extractSitemapButtonButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int opt = JOptionPane.showConfirmDialog(extractSitemapButtonButton,
                        "点击确认将从网站地图文件（sitemap.xml）中提取链接，但是请注意：\n\n" +
                                "Indexing 接口的本意是：新内容产生时，及时通知搜索引擎爬取，确保新内容的及时收录。\n" +
                                "网站地图包含了全站连接，其中包括陈旧的内容，所以会产生以下问题：\n\n" +
                                "1.Indexing 接口滥用，因为提交的并不是新产生的内容，包含大量陈旧可能违反搜索引擎的用户使用协议，可能导致接口权限被收回\n" +
                                "2.全站链接数量过大，部分接口每天每月有使用限额，一口气提交会导致接口使用额度耗尽\n\n" +
                                "请知晓以上说明，自行合理的使用 API。",
                        "从站点地图中提取链接",
                        JOptionPane.YES_NO_OPTION, WARNING_MESSAGE);
                if (opt == JOptionPane.YES_OPTION) {
                    //确认继续操作
                    setLog("从站点地图中提取链接");
                    Thread execService = new Thread(new ExtractSitemapService(mainWindow), "ExtractSitemapService");
                    execService.start();
                }
            }
        });
        ConfigVO configVO = ConfigFileService.getConfig();
        if (configVO != null) {
            siteUrl.setText(configVO.getSiteUrl());
            baiduToken.setText(configVO.getBaiduToken());
            bingToken.setText(configVO.getBingToken());
            googleJson.setText(configVO.getGoogleJsonPath());
            soToken.setText(configVO.getSoToken());
        }
    }

    public void setLog(String log) {
        logText.append("\r\n" + DateUtils.getDate("yyyy-MM-dd HH:mm:ss") + " : " + log);
    }

    public List<String> getUrlTexts() throws BadLocationException {
        List<String> stringList = new ArrayList<>(urls.getRows());
        for (int i = 0; i < urls.getLineCount(); i++) {
            int start = urls.getLineStartOffset(i);
            int end = urls.getLineEndOffset(i);
            String str = urls.getText(start, end - start).replace("\r\n", "").replace("\n", "");
            if (!BeanUtils.isEmpty(str)) {
                if (str.startsWith(siteUrl.getText())) {
                    stringList.add(str);
                } else {
                    setLog("检测到【" + str + "】与站点URL不符，跳过处理。");
                }
            }
        }
        return stringList;
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout(0, 0));
        final JSplitPane splitPane1 = new JSplitPane();
        splitPane1.setContinuousLayout(true);
        splitPane1.setDividerLocation(400);
        splitPane1.setDividerSize(5);
        mainPanel.add(splitPane1, BorderLayout.CENTER);
        rightSplitPane = new JSplitPane();
        rightSplitPane.setDividerLocation(300);
        rightSplitPane.setDividerSize(5);
        rightSplitPane.setOrientation(0);
        splitPane1.setRightComponent(rightSplitPane);
        urlsScroPane = new JScrollPane();
        rightSplitPane.setLeftComponent(urlsScroPane);
        urls = new JTextArea();
        urls.setEditable(true);
        urls.setText("https://www.renfei.net/demo/1");
        urlsScroPane.setViewportView(urls);
        logsScroPane = new JScrollPane();
        rightSplitPane.setRightComponent(logsScroPane);
        logText = new JTextArea();
        logText.setBackground(new Color(-1118482));
        logText.setEditable(false);
        logText.setText("--== 运行日志 ==--");
        logsScroPane.setViewportView(logText);
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new GridLayoutManager(16, 2, new Insets(0, 0, 0, 0), 2, 4));
        splitPane1.setLeftComponent(panel1);
        final JLabel label1 = new JLabel();
        label1.setText("百度Token：");
        panel1.add(label1, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(30, 16), null, 0, false));
        final JLabel label2 = new JLabel();
        label2.setText("必应Token：");
        panel1.add(label2, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(30, 16), null, 0, false));
        final JLabel label3 = new JLabel();
        label3.setText("上报项：");
        panel1.add(label3, new GridConstraints(8, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(30, 16), null, 0, false));
        chkBaiduPuTong = new JCheckBox();
        chkBaiduPuTong.setSelected(true);
        chkBaiduPuTong.setText("百度普通收录");
        panel1.add(chkBaiduPuTong, new GridConstraints(8, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(30, 21), null, 0, false));
        chkBaiDuKuiSu = new JCheckBox();
        chkBaiDuKuiSu.setText("百度快速收录");
        panel1.add(chkBaiDuKuiSu, new GridConstraints(9, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(30, 21), null, 0, false));
        chkGoogle = new JCheckBox();
        chkGoogle.setText("谷歌搜索");
        panel1.add(chkGoogle, new GridConstraints(11, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(30, 21), null, 0, false));
        execButton = new JButton();
        execButton.setText("执行");
        panel1.add(execButton, new GridConstraints(13, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(30, 30), null, 0, false));
        final JLabel label4 = new JLabel();
        label4.setText("谷歌私钥：");
        panel1.add(label4, new GridConstraints(3, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(30, 16), null, 0, false));
        final JLabel label5 = new JLabel();
        label5.setText("使用说明：");
        panel1.add(label5, new GridConstraints(14, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(30, 16), null, 0, false));
        chkBing = new JCheckBox();
        chkBing.setText("必应搜索");
        panel1.add(chkBing, new GridConstraints(10, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(30, 21), null, 0, false));
        siteUrl = new JTextField();
        siteUrl.setText("https://www.renfei.net");
        panel1.add(siteUrl, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(30, 30), null, 0, false));
        final JLabel label6 = new JLabel();
        label6.setText("站点URL：");
        panel1.add(label6, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(30, 16), null, 0, false));
        baiduToken = new JTextField();
        panel1.add(baiduToken, new GridConstraints(1, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(30, 30), null, 0, false));
        bingToken = new JTextField();
        panel1.add(bingToken, new GridConstraints(2, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(30, 30), null, 0, false));
        explain = new JTextArea();
        explain.setBackground(new Color(-1118482));
        explain.setEditable(false);
        explain.setLineWrap(true);
        explain.setText("1.百度Token获取地址： https://ziyuan.baidu.com/linksubmit/index\n2.必应Token获取地址：https://docs.microsoft.com/en-us/bingwebmaster/getting-access\n3.谷歌JSON私钥获取：https://www.renfei.net/posts/1003342\n4.谷歌上报需要本地是翻墙状态，否则网络不通\n5.各个平台的接口提交配额与本工具无关，是各个平台分配给你的；例如百度快速收录是百度站长工具给予的权限，与是否使用本工具无关\n6.本工具不会收集上报用户的Token，本工具代码已开源，欢迎监督，如遇仿制程序上报Token请联系 i@renfei.net");
        explain.setWrapStyleWord(true);
        panel1.add(explain, new GridConstraints(15, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_WANT_GROW, null, new Dimension(30, 50), null, 0, false));
        googleJson = new JButton();
        googleJson.setText("点击选择JSON文件");
        panel1.add(googleJson, new GridConstraints(3, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(30, 30), null, 0, false));
        saveConfig = new JCheckBox();
        saveConfig.setSelected(true);
        saveConfig.setText("将配置保存到本地，下次自动加载");
        panel1.add(saveConfig, new GridConstraints(5, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(30, 21), null, 0, false));
        final JLabel label7 = new JLabel();
        label7.setText("360搜索Token：");
        panel1.add(label7, new GridConstraints(4, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        soToken = new JTextField();
        panel1.add(soToken, new GridConstraints(4, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(30, 30), null, 0, false));
        chkSo = new JCheckBox();
        chkSo.setText("360搜索");
        panel1.add(chkSo, new GridConstraints(12, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(30, 21), null, 0, false));
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return mainPanel;
    }

}
